-- NewDungeonTypeController
-- 地下城类型选择controler

local NewDungeonTypeView = import("..views.NewDungeonTypeView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(params)
	self.super.ctor(self, params)
end

function ClassRef:init()
	self._model = app:getInst("TeamModel")

	-- self._refreshRewardHandler = UD:onChanged("activeRewards", handler(self, self.refreshRewardStatus))
	self._walletHandler = UD:onChanged("wallet", handler(self, self.updatePlayer))
end

function ClassRef:removeListeners()
	-- UD:unChanged("activeRewards", self._refreshRewardHandler)
	UD:unChanged("wallet", self._walletHandler)
end

function ClassRef:listEvent()
    return {}
end

function ClassRef:onEvent(eventName, data)
	-- if eventName == "" then
	-- end
end

function ClassRef:openView(data)
	if cc.isDead(self._view) then
		self._view = NewDungeonTypeView.new( app.mvc.VIEWTYPE_BLUR )
		self._view:onCloseView(handler(self, self.closeView))
		self._view:onClearView(handler(self, self.clearView))
		self._view:onOperate(handler(self, self.onOperate))
	end
	self:addChild(self._view)

	local confs = GD:queryTeamTargetSurfacesByType(Enums.TeamMatchType.Dungeon)
	local openLvDict = self._model:getTargetTypeOpenLvDict(Enums.TeamMatchType.Dungeon)
	self._view:initTypeList(confs, openLvDict)
	-- self:refreshRewardStatus()

	app:sendMsg( "PlayerController", "showExpBar", SceneZOrder.upExpBarZOrder )
end

-- 刷新奖励装填 (pwrd未使用)
function ClassRef:refreshRewardStatus()
	do return end
	
	UD.redPoint:checkStateByKey(RedPointConstant.Dungeon_ExpCard)
	if not self:isViewAlive() then return end
	self._view:refreshRewardStatus()
end

-- 刷新宝箱数据
function ClassRef:updatePlayer(name, old, new)
    if not self:isViewAlive() then 
		return 
	end
    if name == "dungeonPoint" or name == "dungeonLuckBox" then
		self._view:refreshDungeonBoxInfo()
	end
end

function ClassRef:closeView(data)
	if self._view then
		self:removeChild(self._view, data)
		self:clearView()
	end
end
function ClassRef:clearView(data)
	if self._view then
		self._view = nil
	end
	app:sendMsg( "PlayerController", "showExpBar" )
end

function ClassRef:openSecretUI()
	local isOpen = UIHandler:isFunctionOpen(Constant.FUNC_TYPE_TEAMMATCHING_SECRET)
	if not isOpen then
		local reason = UIHandler:getFunctionOpenCondition(Constant.FUNC_TYPE_TEAMMATCHING_SECRET)
		display.pushToast(reason)
		return
	end

	local hController = UIHandler:getSystemUiByKey("m_secretController")
    if not hController then
        local SecretController = app:getClass("SecretController")
        UIHandler:getSystemUi():addSystemUI("m_secretController",SecretController.new())
    else
        hController:setVisible(true)
	end

	-- UIHandler:getSystemUi():removeSystemUiByKey("m_secretController")
end

function ClassRef:openMatchingView(data)
	local teamTaregtVo = self._model:getTeamTargetVo()
	local params = {matchType=Enums.TeamMatchType.Dungeon, targetType=data}
	-- 如果打开的类型正好有当前队伍目标，则选中目标(秘境除外，秘境始终不需要填目标)
	if data ~= Enums.TeamTargetType.secret and teamTaregtVo and teamTaregtVo.type == data then
		params.targetId = teamTaregtVo.id
	end
	UIHandler:handleMessage({id=Constant.FUNC_TYPE_TEAMMATCHING, mode="open", data=params})
end

function ClassRef:onOperate(op, ...)
	-- print("NewDungeonTypeController.onOperate op", op)
	-- dump({...}, "NewDungeonTypeController.onOperate data")
	local data = ...
	if op == "dungeonsType_selected" then -- 地下城选择了具体的玩法
		if data == 4 then --秘境
			self:openSecretUI()
		else
			self:openMatchingView(data)
		end
	elseif op == "dungeonsType_previewReward" then -- 预览地下城累积经验宝箱
		local RewardModel = app:getInst("RewardModel")
		RewardModel:reqExpBoxReward( data )
	elseif op == "dungeonsType_previewBoxReward" then -- 预览地城宝箱
		app:sendMsg("BoxController", "openView", { boxType = Enums.BoxType.dungeon })
	end
end

return ClassRef
